var sysMenuList = {};
var SysMenuList = function () {
    var $this = this;
    //初始化
    this.init = function () {
        //设置树形属性
        var setting = {
            isSimpleData : true,              //数据是否采用简单 Array 格式，默认false
            view: {
                selectedMulti: false
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey : "id", //id
                    pIdKey : "parentId", //父节点的id :pid的名称 ,默认 Pid
                    rootPId : 0 //根节点为空表示最高节点
                },
                key : {
                    name : "menuName"        //修改属性名称
                }
            },
            edit: {
                enable: true,
                showRemoveBtn:false,
                showRenameBtn:false
            },
            callback: {
                onClick: $this.zTreeOnClickcar,  //点击事件
                beforeDrag: $this.zTreeBeforeDrag,
                beforeDrop: $this.zTreeBeforeDrop
            }
        };
        //加载树形
        layuiFunciton.ajaxRequest({
            url:layuiFunciton.URL + "/sysMenu/findSysMenuAll",
            method:"post",
            success: function(data){
                $.fn.zTree.init($("#treeDemo"), setting, data);
            }
        });
        //实例化表格
        layuiFunciton.tableInit({
            toolbar:'.btn',
            tableId: 'sysMenu',
            url:  layuiFunciton.URL + '/sysMenu/selectSysMenuPage',
            id: 'sysMenuTabelId',
            searchFromId:'searchFrom',
            tableFilter:'newsList',  //表格监听名称,
            cols: [[
                {type:'numbers',title: '序号',fixed: 'left'},
                {field:'menuName', title: '模块名称',width:150 , sort: true}
                ,{field:'menuCode', title: '模块编号',width:150 , sort: true}
                ,{field:'menuUrl', title: '访问路径', sort: true}
                ,{field:'menuLevel', title: '模块层级',width:110 ,  sort: true}
                ,{field:'enabled', title: '模块状态',width:110 , sort: true,templet:function (data) {
                    var html = '<input type="checkbox" name="enabled" value="'+data.id+'" lay-skin="switch" lay-text="启用|禁用" lay-filter="enabledDemo" ' ;
                    if(data.enabled==1){
                        html+='checked';
                    }
                    html+='>';
                    return html;
                }}
                ,{field:'createTime', title: '创建时间',width:200 ,  sort: true}
            ]],
            tools:{
                edit:function (rowData) {
                    layuiFunciton.layerOpen({id:"123",title:"修改",content:"edit.html",data:rowData,end:function () {
                        layuiFunciton.refresh('searchFrom','sysMenuTabelId');
                    }});
                }
                ,del:function (rowData) {
                    layuiFunciton.alert({types:'confirm',title:'是否删除菜单？',success:function () {
                        layuiFunciton.ajaxRequest({
                            url: layuiFunciton.URL + '/sysMenu/updateSysMenu',
                            method:'post',
                            data:{id:rowData.id,delFlag:1},
                            success:function (data) {
                                layuiFunciton.refresh('searchFrom','sysMenuTabelId');
                            }
                        });
                    }});
                }
                ,addMenu_btn:function (rowData) {//添加菜单
                    if($("#menuLevel").val()>0)
                        layuiFunciton.layerOpen({title:"添加",content:"add.html",finder:'form',data:{menuLevel:(1+Number($("#menuLevel").val())) , parentId:$("#id").val()},end:function () {
                            layuiFunciton.refresh('searchFrom','sysMenuTabelId');
                        }});
                    else
                        layuiFunciton.alert({types:'warn',title:'请选择添加位置！'});
                }
            }
            ,filter:[
                {
                    types:'form',   //   注：仅支持 from 、table 两种类型  监听 table的 tools 请使用 tools 属性
                    filterTypes: 'switch',   //
                    filterName: 'enabledDemo',
                    returnData : function (obj) {
                        layuiFunciton.ajaxRequest({
                            url: layuiFunciton.URL + '/sysMenu/updateSysMenu',
                            method:'post',
                            data:{id:obj.value,enabled:obj.elem.checked?1:0},
                            success:function (data) {
                                layuiFunciton.refresh('searchFrom','sysMenuTabelId');
                            }
                        });
                    }
                }/*,{
                 types:'table',   //   注：仅支持 from 、table 两种类型  监听 table的 tools 请使用 tools 属性  table 不用填写 filterName属性  表格监听器名称
                 filterTypes: 'edit',  //       例如 ：  table.on('edit(test3)', function(obj){}   对应属性    types.on('filterTypes(enabledDemo)')
                 returnData : function (obj) {
                 layer.alert('阿斯顿发生大法师的发：<br>'+ JSON.stringify(obj));
                 }
                 }*/
            ]
        });
    };
    //绑定事件
    this.event =function () {
        //搜索
        $("#search_btn").click(function () {
            layuiFunciton.refresh('searchFrom','sysMenuTabelId',1);
        });

    };
    //点击树形触发
    this.zTreeOnClickcar = function (event, treeId, treeNode) {
        $("#id").val(treeNode.id);
        $("#menuLevel").val(treeNode.menuLevel);
        layuiFunciton.refresh('searchFrom','sysMenuTabelId');
    };
    //移动知识节点前处理
    this.zTreeBeforeDrag = function (treeId, treeNodes) {
        for (var i=0,l=treeNodes.length; i<l; i++) {
            var  pid = treeNodes[i].pId;
            /*            if(pid=="root"||pid==null||pid=="null"){
             alert("只能移动叶子节点。");
             return false;
             }
             */
        }
        return true;
    };
    //拖拽释放之后结束前执行   moveType值类型请参考api moveType 移动之后的位置  inner 里面   prev 上边  next 下边
    this.zTreeBeforeDrop = function (treeId, treeNodes, targetNode, moveType) {
        if(targetNode.menuLevel==3 && moveType=='inner'){
            layuiFunciton.warn('不可移动到第四级！');
            return false;
        }else{
            var menuObj = treeNodes[0];
            if(moveType=='inner'){
                menuObj.sortIndex=targetNode.sortIndex;
                menuObj.parentId=targetNode.id;
                menuObj.menuLevel=targetNode.menuLevel+1;
            }else if(moveType=='prev'){
                menuObj.sortIndex=targetNode.sortIndex;
                menuObj.menuLevel=targetNode.menuLevel;
                menuObj.parentId=targetNode.parentId;
            }else{
                menuObj.sortIndex=targetNode.sortIndex+1;
                menuObj.menuLevel=targetNode.menuLevel;
                menuObj.parentId=targetNode.parentId;
            }
            layuiFunciton.ajaxRequest({
                url:  layuiFunciton.URL + '/sysMenu/updateSysMenu',
                method: 'post',
                data: {moveType:moveType,id:menuObj.id,parentId:menuObj.parentId,sortIndex:menuObj.sortIndex,menuLevel:menuObj.menuLevel},
                success: function(data){

                }
            });
        }
    };
};
$(function () {
    sysMenuList = new SysMenuList();
    sysMenuList.init();
    sysMenuList.event();
});